from PyQt4 import QtGui,QtCore
from ftllibs.FTL import RUNES


class RunesMenu_Button(QtGui.QWidget):
    clicked = QtCore.pyqtSignal(['QString'])
    def __init__(self,runename):
        super(RunesMenu_Button, self).__init__()
        self.runename = runename
        self.setFixedSize(390,84)
        self.setContentsMargins(0,0,0,0)
        self.setStyleSheet('margin: 0px;border 0px')
        self.initUI()

    def initUI(self):
        self.MasterLayout = QtGui.QGridLayout()
        self.PixMapLabel = QtGui.QLabel()
        # Icon
#        ItemIcon=ITEMS.ITEMS[self.itemid].Icon
        self.RunesIcon = QtGui.QPixmap()
        self.RunesIcon.loadFromData(RUNES.RUNES[self.runename].IconData)
        self.PixMapLabel.setPixmap(self.RunesIcon)
        # Name
#        RunesName=ITEMS.ITEMS[self.itemid].Name
        self.RunesNameLabel=QtGui.QLabel()
        self.RunesNameLabel.setText("<h3>%s</h3>" % (RUNES.RUNES[self.runename].Name))
        self.RunesNameLabel.setStyleSheet('color: Black')
	self.RunesNameLabel.setFixedWidth(300)
#        self.RunesNameLabel.setWordWrap(True)
        # TotalCosts
        RunesDescription=str(RUNES.RUNES[self.runename].Description)
        self.RunesTotalCostsLabel=QtGui.QLabel()
        self.RunesTotalCostsLabel.setText("<h4>%s</h4>" % (RunesDescription))
        self.RunesTotalCostsLabel.setStyleSheet('color: red')
        self.RunesTotalCostsLabel.setWordWrap(True)
        # Layout
        self.MasterLayout.addWidget(self.PixMapLabel,0,0,2,1,QtCore.Qt.AlignLeft)
        self.TextLayout = QtGui.QVBoxLayout()
        self.TextLayout.addWidget(self.RunesNameLabel)
        self.TextLayout.addWidget(self.RunesTotalCostsLabel)
        self.TextLayoutSpacer=QtGui.QHBoxLayout()
        self.TextLayoutSpacer.addLayout(self.TextLayout)
        self.TextLayoutSpacer.addStretch(1)
        self.MasterLayout.addLayout(self.TextLayoutSpacer,0,1,1,1,QtCore.Qt.AlignLeft)
#       self.MasterLayout.addWidget(self.RunesTotalCostsLabel,1,1,1,1,QtCore.Qt.AlignLeft)
        self.setLayout(self.MasterLayout)
        self.setToolTip(RUNES.RUNES[self.runename].Description)

    def mousePressEvent(self, event):
        print "RunesMenu_Button: Emitting 'clicked':\"%s\"" % (self.runename)
        self.clicked.emit(self.runename)

    def mouseMoveEvent(self,e):
        print "RunesMenu_Button: Emittig 'dragEvent':\"%s\"" % (self.runename)
        mimeData = QtCore.QMimeData()
        mimeData.setData('text/plain',self.runename)
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)
        drag.setPixmap(self.RunesIcon)
        drag.setHotSpot(e.pos() - self.rect().topLeft())
        drag.start(QtCore.Qt.IgnoreAction)

## RunesMenu
## These are All Runess in the Game, split up in tabs for each tag
class RunesMenu(QtGui.QWidget):
    addRunesPressed = QtCore.pyqtSignal(['QString'])
    def __init__(self,parent=None):
        super(RunesMenu,self).__init__()
        self.MasterLayout = QtGui.QHBoxLayout()
        self.setSizePolicy(QtGui.QSizePolicy.Fixed,QtGui.QSizePolicy.Expanding)
        self.setLayout(self.MasterLayout)
        self.setFixedWidth(440)
        self.initUI()

    def initUI(self):
        self.RunesTabs = QtGui.QTabWidget()
        # defense -> health

        self.MarkScroll = QtGui.QScrollArea()
        self.MarkWidget = QtGui.QWidget()
        self.MarkWidget.setContentsMargins(0,0,0,0)
        self.MarkWidget.setStyleSheet('margin: 0px; border: 0px')
        self.MarkLayout = QtGui.QGridLayout()
        self.MarkLayout.setHorizontalSpacing(0)
        self.MarkLayout.setVerticalSpacing(0)
        
        self.SealScroll = QtGui.QScrollArea()
        self.SealWidget = QtGui.QWidget()
        self.SealWidget.setContentsMargins(0,0,0,0)
        self.SealWidget.setStyleSheet('margin: 0px; border: 0px')
        self.SealLayout = QtGui.QGridLayout()
        self.SealLayout.setHorizontalSpacing(0)
        self.SealLayout.setVerticalSpacing(0)

        self.GlyphScroll = QtGui.QScrollArea()
        self.GlyphWidget = QtGui.QWidget()
        self.GlyphWidget.setContentsMargins(0,0,0,0)
        self.GlyphWidget.setStyleSheet('margin: 0px; border: 0px')
        self.GlyphLayout = QtGui.QGridLayout()
        self.GlyphLayout.setHorizontalSpacing(0)
        self.GlyphLayout.setVerticalSpacing(0)

        self.QuintessenceScroll = QtGui.QScrollArea()
        self.QuintessenceWidget = QtGui.QWidget()
        self.QuintessenceWidget.setContentsMargins(0,0,0,0)
        self.QuintessenceWidget.setStyleSheet('margin: 0px; border: 0px')
        self.QuintessenceLayout = QtGui.QGridLayout()
        self.QuintessenceLayout.setHorizontalSpacing(0)
        self.QuintessenceLayout.setVerticalSpacing(0)
        
        self.Buttons = []
        for i in RUNES.GetList('mark'):
            self.Buttons.append(RunesMenu_Button(i))
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 1:
                x = 0
                y += 1
            self.MarkLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.RunesMenu_Button_clicked)
        self.MarkWidget.setLayout(self.MarkLayout)
        self.MarkScroll.setWidget(self.MarkWidget)

        self.Buttons = []
        for i in RUNES.GetList('seal'):
            self.Buttons.append(RunesMenu_Button(i))
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 1:
                x = 0
                y += 1
            self.SealLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.RunesMenu_Button_clicked)
        self.SealWidget.setLayout(self.SealLayout)
        self.SealScroll.setWidget(self.SealWidget)

        self.Buttons = []
        for i in RUNES.GetList('glyph'):
            self.Buttons.append(RunesMenu_Button(i))
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 1:
                x = 0
                y += 1
            self.GlyphLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.RunesMenu_Button_clicked)
        self.GlyphWidget.setLayout(self.GlyphLayout)
        self.GlyphScroll.setWidget(self.GlyphWidget)
        
        self.Buttons = []
        for i in RUNES.GetList('quintessence'):
            self.Buttons.append(RunesMenu_Button(i))
        x = 0
        y = 1
        for i in self.Buttons:
            if x == 1:
                x = 0
                y += 1
            self.QuintessenceLayout.addWidget(i,y,x)
            x += 1
            i.clicked.connect(self.RunesMenu_Button_clicked)
        self.QuintessenceWidget.setLayout(self.QuintessenceLayout)
        self.QuintessenceScroll.setWidget(self.QuintessenceWidget)
        self.RunesTabs.addTab(self.MarkScroll,"Marks")
        self.RunesTabs.addTab(self.SealScroll,"Seal")
        self.RunesTabs.addTab(self.GlyphScroll,"Glyph")
        self.RunesTabs.addTab(self.QuintessenceScroll,"Quintessence")
        self.MasterLayout.addWidget(self.RunesTabs)

    def RunesMenu_Button_clicked(self,runename):
        print runename
        self.addRunesPressed.emit(runename)

#######################################################
